<script lang="ts" setup>
import { message } from 'ant-design-vue';
import { useDownExcel } from '../useDownExcel';

interface CompanyName_Item {
  name: string
  key: string
}

const columns: any = [
  {
    title: '区县公司',
    dataIndex: 'company',
    align: 'center',
    width: '10%',
    key: 'company',
  },
  {
    title: '考核得分',
    dataIndex: 'score',
    children: [
      {
        title: '光网建设',
        dataIndex: '光网建设',
        key: '光网建设考核',
        width: 200,
        align: 'center',
      },
      {
        title: '主流终端利旧率',
        dataIndex: '主流终端利旧率',
        key: '主流终端利旧率考核',
        width: 200,
        align: 'center',
      },
      {
        title: '装维考核',
        dataIndex: '装维考核',
        key: '装维考核情况',
        width: 200,
        align: 'center',
      },
      {
        title: '赔补考核',
        dataIndex: '赔补考核',
        key: '改迁赔补考核',
        width: 200,
        align: 'center',
      },
      {
        title: '政企交付满意度',
        dataIndex: '政企交付满意度',
        key: '交付满意度考核',
        width: 200,
        align: 'center',
      },
      {
        title: '网信安考核',
        dataIndex: '网信安考核',
        key: '网信安考核情况',
        width: 200,
        align: 'center',
      },
    ],
    align: 'center',
    width: '20%',
    key: 'score',
  },
  {
    title: '小计',
    dataIndex: 'sum',
    align: 'center',
    width: '10%',
    key: 'sum',
    sorter: (a: any, b: any) => Number(a.sum) - Number(b.sum),
  },
];
const loading = ref<boolean>(false);
const assessmentName: any[] = [{ name: '光网建设', key: '光网建设考核' }, { name: '主流终端利旧率', key: '主流终端利旧率考核' }, { name: '装维考核', key: '装维考核情况' }, { name: '赔补考核', key: '改迁赔补考核' }, { name: '政企交付满意度', key: '交付满意度考核' }, { name: '网信安考核', key: '网信安考核情况' }];
const company: CompanyName_Item[] = [{ name: '越城', key: 'yuecheng' }, { name: '柯桥', key: 'keqiao' }, { name: '上虞', key: 'shangyu' }, { name: '嵊州', key: 'shengzhou' }, { name: '新昌', key: 'xinchang' }, { name: '诸暨', key: 'zhuji' }];
const data = ref<any[]>([]);
const lastSpan = ref<string>('');
const score = ref<any[]>([]);
const sData = ref<any[]>([]);
function removeIsDelete<T extends { isDelete?: any }>(data: T): Omit<T, 'isDelete'> {
  // 使用TypeScript的Omit工具类型来确保返回类型不包含isDelete
  const { isDelete, ...rest } = data;
  // 注意：我们实际上并没有对isDelete做任何处理，它只是没有被包含在rest中
  return rest;
}
async function getScore(name: string) {
  lastSpan.value = '';
  score.value = [];
  sData.value = [];
  // 获取分数
  loading.value = true;
  await getQuarterBySheetName(name).then((res: any) => {
    sData.value = res.data;
    const firstData = removeIsDelete(sData.value[0]);
    for (const key in firstData) {
      if (Object.prototype.hasOwnProperty.call(firstData, key) && firstData[key] !== '') {
        lastSpan.value = key; // 更新最后一个非空字段名
      }
    }
    // 获取数据
    for (let i = 0; i < sData.value.length; i++) {
      if (sData.value[i].isField === 'false') {
        if (lastSpan.value) {
          const scoredata = {
            company: sData.value[i].generalFieldA,
            score: sData.value[i][lastSpan.value],
          };
          score.value.push(scoredata);
        }
      }
    }
  }).catch(() => {
    message.error('数据获取失败！');
  }).finally(() => {
    loading.value = false;
  });
}
const allScore = ref<any[]>([]);
async function getAllScore() {
  for (let i = 0; i < assessmentName.length; i++) {
    await getScore(assessmentName[i].key);
    const s = score.value;
    allScore.value.push({
      name: assessmentName[i].name,
      score: s,
    });
  }
}
function isNumber(num: string) {
  if (Number(num)) {
    return Number(num);
  }
  else {
    return 0;
  }
}
async function getData() {
  const result: any = [];
  for (let i = 0; i < company.length; i++) {
    result.push({ company: company[i].name });
    // result['company'] = company[i].name
  }
  for (let j = 0; j < assessmentName.length; j++) {
    for (let z = 0; z < allScore.value.length; z++) {
      if (allScore.value[z].name === assessmentName[j].name) {
        for (let k = 0; k < allScore.value[z].score.length; k++) {
          result.forEach((company: any) => {
            if (company.company === allScore.value[z].score[k].company) {
              company[assessmentName[j].name] = allScore.value[z].score[k].score;
            }
          });
        }
      }
    }
  }
  result.forEach((company: any) => {
    let sum = 0;
    assessmentName.forEach((aname: any) => {
      sum += isNumber(company[aname.name]);
    });
    company.sum = sum;
  });
  data.value = result;
}
onMounted(async () => {
  await getAllScore();
  getData();
});
</script>

<template>
  <PubPageMain class="max">
    <div class="header">
      <div class="left">
        <a-space>
          <div class="pjTitle">
            区县季度评价汇总
          </div>
        </a-space>
      </div>
      <div class="right">
        <a-space>
          <a-button type="primary" @click="useDownExcel().downSummaryExcel()">
            导出为excel
          </a-button>
          <a-button type="primary" @click="$router.push('/assessment_indicators/quarter')">
            返回
          </a-button>
        </a-space>
      </div>
    </div>
    <div class="content">
      <a-table
        :columns="columns" :data-source="data" row-key="id" :scroll="{ x: 1200 }" class="q-table"
        :loading="loading" :pagination="false" bordered
      >
        <template #headerCell="{ column }">
          <div v-if="!['company', 'score', 'sum'].includes(column.dataIndex as string)">
            <RouterLink
              :to="{ name: 'assessmentIndicatorsQuarterSummaryDetails', query: { assessmentName: column.key as string } }"
            >
              {{ column.title }}
            </RouterLink>
          </div>
          <div v-else>
            {{ column.title }}
          </div>
        </template>
      </a-table>
    </div>
  </PubPageMain>
</template>

<style lang="scss" scoped>
.max {
  display: flex;
  flex-direction: column;
  max-width: 100%;
  max-height: 95%;
  overflow: auto;

  .header {
    position: sticky;
    top: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    height: 40px;
    margin-bottom: 10px;
    background-color: #fff;

    .pjTitle {
      padding-left: 15px;
      background: url("@/assets/images/title-left-bg.svg") no-repeat;
    }

    .right {
      display: flex;
      align-items: center;
    }
  }

  .content {
    display: flex;
    flex: 1;
    flex-direction: column;
    width: 100%;
    height: 95%;
    overflow: auto;
    overflow-y: auto;

    .m-table {
      flex-grow: 1;
      height: 100%;
      max-height: 100%;
      padding: 10px;
    }
  }
}

.editable-row-operations a {
  margin-right: 8px;
}
</style>
